package com.das.mechanic_base.adapter.task;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.task.FollowCarAllBean;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3TaskUserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<FollowCarAllBean.ListBean> mList;

    private List<Boolean> bList;
    private final Drawable noDraw;

    public X3TaskUserAdapter(Context mContext) {
        this.mContext = mContext;
        bList = new ArrayList<>();
        mList = new ArrayList<>();
        noDraw = mContext.getResources().getDrawable(R.mipmap.x3_no_search_result_icon);
        noDraw.setBounds(0, 0, noDraw.getMinimumWidth(), noDraw.getMinimumHeight());
    }

    public void changeUser(List<FollowCarAllBean.ListBean> list) {
        this.mList = list;
        bList.clear();
        for (int i = 0; i < mList.size(); i++) {
            bList.add(i, false);
        }
        notifyDataSetChanged();
    }

    public List<FollowCarAllBean.ListBean> getmList() {
        return mList;
    }

    public void changeAddUser(List<FollowCarAllBean.ListBean> list) {
        if (mList == null) {
            mList = new ArrayList<>();
        }
        mList.addAll(list);
        bList.clear();
        for (int i = 0; i < mList.size(); i++) {
            bList.add(i, false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_home_item,parent,false));
        }
        return new TaskUserHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_task_user_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == 0) {
            EmptyHolder emptyHolder = (EmptyHolder) holder;
            emptyHolder.rl_create.setVisibility(View.GONE);
            emptyHolder.rl_quick.setVisibility(View.GONE);
            emptyHolder.tv_empty.setText(mContext.getString(R.string.x3_no_search_data));
            emptyHolder.tv_empty.setCompoundDrawables(null,noDraw,null,null);

        }else {
            TaskUserHolder userHolder = (TaskUserHolder) holder;

            X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(mList.get(position).getImgUrl()), userHolder.iv_header, R.mipmap.x3_car_owner_header);

            String carOwnerName = X3StringUtils.isEmpty(mList.get(position).getName()) ? "" : mList.get(position).getName();
            String mobile = X3StringUtils.isEmpty(mList.get(position).getMobile()) ? "" : mList.get(position).getMobile();
            userHolder.tv_name.setText(carOwnerName + " (" + mobile + ")");

            List<String> carNumSplit = mList.get(position).getCarNumList();
            if (X3StringUtils.isListEmpty(carNumSplit)) {
                userHolder.tv_car.setText("");
            } else {
                String content = "";
                for (int i = 0; i < carNumSplit.size(); i++) {
                    if (i == 0) {
                        content = carNumSplit.get(i);
                    } else {
                        content = content + ", " + carNumSplit.get(i);
                    }
                }
                userHolder.tv_car.setText(content);
            }

            if (bList.get(position)) {
                userHolder.iv_select.setImageResource(R.mipmap.x3_iv_task_status_select);
            } else {
                userHolder.iv_select.setImageResource(R.mipmap.x3_iv_task_status_unsele);
            }

            userHolder.itemView.setOnClickListener(view -> {

                for (int i = 0; i < bList.size(); i++) {
                    if (i == position) {
                        if (bList.get(i)) {
                            bList.remove(i);
                            bList.add(i, false);
                        } else {
                            bList.remove(i);
                            bList.add(i, true);
                        }
                    } else {
                        bList.remove(i);
                        bList.add(i, false);
                    }
                }
                notifyDataSetChanged();

                if (iOnTaskUserSelect != null) {
                    iOnTaskUserSelect.iOnTaskSlelect(bList.get(position) ? mList.get(position) : null);
                }
            });

            userHolder.iv_header.setOnClickListener(view -> {
                // 跳转到车主 用户详情页
                Bundle bundle = new Bundle();
                bundle.putBoolean("isDark",true);
                bundle.putString("title","客户详情页");
                bundle.putString("path", X3Constent.HOME_CUSETOM_DETAIL_URL + mList.get(position).getId());
                X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);

//                Intent intent = new Intent(mContext, GroundPushActivity.class);
//                intent.putExtra("isDark", true);
//                intent.putExtra("title","客户详情页");
//                intent.putExtra("path", Constent.HOME_CUSETOM_DETAIL_URL + mList.get(position).getId());
//                mContext.startActivity(intent);
            });
        }
    }

    IOnTaskUserSelect iOnTaskUserSelect;

    public void setiOnTaskUserSelect(IOnTaskUserSelect iOnTaskUserSelect) {
        this.iOnTaskUserSelect = iOnTaskUserSelect;
    }

    public interface IOnTaskUserSelect {
        void iOnTaskSlelect(FollowCarAllBean.ListBean userBean);
    }

    @Override
    public int getItemViewType(int position) {
        if (X3StringUtils.isListEmpty(mList)) {
            return 0;
        }
        return 1;
    }

    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? 1 : mList.size();
    }

    class TaskUserHolder extends RecyclerView.ViewHolder {
        ImageView iv_header;
        TextView tv_name;
        TextView tv_car;
        ImageView iv_select;

        public TaskUserHolder(@NonNull View itemView) {
            super(itemView);
            iv_header = itemView.findViewById(R.id.iv_header);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_car = itemView.findViewById(R.id.tv_car);
            iv_select = itemView.findViewById(R.id.iv_select);
        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;
        RelativeLayout rl_create;
        RelativeLayout rl_quick;
        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
            rl_create = itemView.findViewById(R.id.rl_create);
            rl_quick = itemView.findViewById(R.id.rl_quick);
        }
    }
}
